//
//  LPPSettingController.m
//  Lottery
//
//  Created by 刘盼 on 16/1/22.
//  Copyright © 2016年 lhheh. All rights reserved.
//

#import "LPPSettingController.h"
#import "LPPSettingCell.h"

@interface LPPSettingController ()

@property(nonatomic,strong)NSArray *settings;
@end

@implementation LPPSettingController

-(NSArray *)settings{
    
    if (!_settings) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:self.plistName ofType:@"plist"];
        
        _settings = [NSArray arrayWithContentsOfFile:path];
    }
    return _settings;
}

#pragma mark--------重写初始化函数，使其返回组的形式

-(instancetype)init{
    
    return [super initWithStyle:UITableViewStyleGrouped];
}

-(instancetype)initWithStyle:(UITableViewStyle)style{
    
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(popViewControllerAnimated:)];
    
    self.navigationItem.leftBarButtonItem = item;
    
}

#pragma mark---------<UITableViewDataSource>-------

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.settings.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSDictionary *group = self.settings[section];
    // 获取cell的数组
    NSArray *item = group[@"items"];
    
    return item.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *group = self.settings[indexPath.section];
    NSArray *items = group[@"items"];
    NSDictionary *item = items[indexPath.row];
    
    LPPSettingCell *cell = [LPPSettingCell settingCellWithtableView:tableView withDictionary:item];
    
    cell.item = item;
    
    return cell;
}

#pragma mark------------<UITableViewDelegate>-----------

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *group = self.settings[indexPath.section];
    NSArray *items = group[@"items"];
    NSDictionary *item = items[indexPath.row];
    //进行版本更新
    if (item[@"funcName"] && [item[@"funcName"]length] > 0) {
        
        NSString *funcName = item[@"funcName"];
        
        SEL funcSEL = NSSelectorFromString(funcName);
        
        [self performSelector:funcSEL withObject:nil];
    }
    //控制器跳转
    if (item[@"targetVc"] && [item[@"targetVc"]length] > 0) {
        
        Class clz = NSClassFromString(item[@"targetVc"]);
        
        UIViewController *Vc = [[clz alloc]init];
        
        if ([Vc isKindOfClass:[LPPSettingController class]]) {
            
            LPPSettingController *vc = (LPPSettingController *)Vc;
            
            vc.plistName = item[@"plistName"];
            
        }
        Vc.title = item[@"title"];
        
        [self.navigationController pushViewController:Vc animated:YES];
    }
}
//组头部视图的文字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSDictionary *group = self.settings[section];
    
    return group[@"headTitle"];
}

//组底部视图的文字
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    
    NSDictionary *group = self.settings[section];
    
    return group[@"footTitle"];
}
#pragma mark---------版本更新-----------
-(void)checkUpVersion {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"当前已是最新版本" preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    
    [self presentViewController:alert animated:YES completion:nil];
}
@end
